<template>
  <div class="goodInfo-box">
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
        <div class="ball" v-show="ballflag" ref="ball"></div>
    </transition>
    <!-- 头部小卡片 -->
    <div class="mui-card">
      <div class="mui-card-header"></div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <swiper :lunbotuList="lunbotuList" :isFull="isFull"></swiper>
        </div>
      </div>
    </div>
    <!-- 商品购买区域 -->
    <div class="mui-card">
      <div class="mui-card-header">{{goodsInfoList.title}}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价:
            <del>￥{{goodsInfoList.market_price}}</del> 销售价:
            <span class="now_price">￥{{goodsInfoList.sell_price}}</span>
          </p>
          <p>
            购买数量:
            <goodsNumBox @getMount="getMount" :max="goodsInfoList"></goodsNumBox>
          </p>
          <mt-button type="primary" size="small">立即购买</mt-button>
          <mt-button type="danger" size="small" @click="addToCar">加入购物车</mt-button>
        </div>
      </div>
    </div>
    <!-- 底部卡片 -->
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号：{{goodsInfoList.goods_no}}</p>
          <p>库存情况：{{goodsInfoList.stock_quantity}}件</p>
          <p>上架时间：{{goodsInfoList.add_time}}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" @click="goDesc(id)">图文介绍</mt-button>
        <mt-button type="danger" size="large" @click="goComment(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>
<script>
import swiper from "../subcomponent/swiper.vue";
import goodsNumBox from "../subcomponent/goodsNumBox.vue";
import { Transform } from 'stream';
export default {
  data() {
    return {
      id: this.$route.query.id, //获取参数上传递过来的id
      lunbotuList: [], //轮播图数组图片
      goodsInfoList:[], //存放商品详情具体信息
      isFull: false,
      ballflag: false,//控制小球显示隐藏标志
      mount:1,  //接收子组件传递过来的数量
    };
  },
  created() {
    this.getlunbotuList();
    this.getGoodsInfo();
  },
  methods: {
    getlunbotuList() {
      //获取轮播图图片放到lunbotuList数组中
      this.$http.get("api/getgoodsInfo?id=" + this.id).then(res => {
        if (res.body.status === 0) {
          //返回数组的每一项增加 img属性 将src图片路径赋值给img属性
          res.body.message.forEach(element => {
            element.img = element.src;
          });
          this.lunbotuList = res.body.message;
        }
      });
    },
    getGoodsInfo(){
      //获取商品详细参数的方法
      this.$http.get('api/goods/info?id='+this.id).then(res=>{
        if(res.body.status===0){
          this.goodsInfoList= res.body.message[0];
        }
      })
    },
    goDesc(id){
        //点击使用编程式导航跳转到图文介绍页面
        this.$router.push({name:'goodsdesc',querys: {id}})
    },
    goComment(id){
        //点击跳转到评论页面
        this.$router.push({name:'goodscomment',querys: {id}})
    },
    addToCar(){
      //点击立即购买执行函数
      this.ballflag=!this.ballflag; //控制小球隐藏显示执行动画
      //拼接出来一个 保存到store的数组对象
      var goodsInfo={
        id:this.id,
        count:this.mount,
        price : this.goodsInfoList.sell_price,
        selected :true
      };
      this.$store.commit('addToShopCar',goodsInfo);
    },
    beforeEnter(el){
      el.style.transform="translate(0,0)"
    },
    enter(el,done){
      el.offsetWidth;
      //获取小球的位置
      const ballPosition=this.$refs.ball.getBoundingClientRect();
      //获取导航栏购物车图标位置
      const badgePosition=document.getElementById('badge').getBoundingClientRect();

      const ballX=badgePosition.left-ballPosition.left;
      const ballY=badgePosition.top-ballPosition.top;

      el.style.transform="translate("+ballX+"px,"+ballY+"px)";

      // el.style.transform='translate(${ballX}px,${ballY}px)';
      el.style.transition="all 0.5s ease";
      done();
    },
    afterEnter(el){
      this.ballflag=!this.ballflag;
    },
    getMount(mount){
      //定义从 goodsNumBox获取的数量值
      this.mount = mount;
      console.log('从子组件获取的数量为:'+this.mount);
    }
  },
  components: {
    swiper,
    goodsNumBox
  }
};
</script>
<style  lang="scss" scoped>
.goodInfo-box {
  background-color: #eee;
  overflow: hidden;
  .now_price {
    color: red;
    font-size: 16px;
    font-weight: bold;
  }
  .mui-card-footer {
      display: block;
      button {
        margin: 10px 0;
      }
  }
}

.ball {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background-color: red;
  position: absolute;
  z-index: 99;
  top: 535px;
  left:142px;
}
</style>

